//
//  FansOrderIndex.swift
//  LeGou
//
//  Created by lxm on 2022/9/4.
//

import UIKit

class FansOrderIndex: ZMViewController {
    //MARK: --------------------属性--------------------
    var sectionVcs = [UIViewController]()
    
    // 分栏title 状态（0已付款、1已结算、2已失效）
    var getTitles : [(String, Int)] {
        return [("全部", -1), ("已付款", 0), ("已结算", 1), ("已失效", 2)]
    }
    
    //MARK: --------------------UI控件--------------------
    var headerContentView = UIView()
    var seg : XMIndexHeaderView!
    var contentView = UIView()
    var childVCCollectioView : UICollectionView!
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = "粉丝订单"
        view.addSubview(headerContentView)
        view.addSubview(contentView)
        makeSeg()
        makeColletionView()
        configData()
        view.backgroundColor = "#F5F5F5".zm.hex()
    }
    
    func addConstraints() {
        headerContentView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(45)
        }
        contentView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(headerContentView.snp.bottom)
        }
        
        childVCCollectioView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    func makeSeg() {
        let config  = XMIndexHeaerViewConfig()
        config.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        config.fontColor  = UIColor.zm.color999999()
        config.selectFont = UIFont.systemFont(ofSize: 16, weight: .medium)
        config.selectFontColr = Style.mainColor
        config.lineViewCornerRadius = 1.5
       
        seg = XMIndexHeaderView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 44), withTitleArray:getTitles.map({$0.0}), animation: false)!
        seg.lineColor = Style.mainColor
        seg.lineWidth = 17
        seg.config = config
        seg.delegateXM = self
        headerContentView.addSubview(seg!)
    }
    
    func makeColletionView() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        childVCCollectioView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 100), collectionViewLayout: layout)
        childVCCollectioView.delegate = self
        childVCCollectioView.dataSource = self
        contentView.addSubview(childVCCollectioView)
        childVCCollectioView.tag = 999
        childVCCollectioView.isPagingEnabled = true
        childVCCollectioView.backgroundColor = .clear
        childVCCollectioView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        childVCCollectioView.showsHorizontalScrollIndicator = false
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    func configData() {
        var i = 0
        for cate in getTitles {
            let vc = FansOrderList()
            vc.type = cate.1
            addChild(vc)
            sectionVcs.append(vc)
            i += 1
        }
    }
    //MARK: --------------------delegate--------------------
}

extension FansOrderIndex : XMIndexHeaderViewDelegate {
    func xmIndexHeaderViewDidselected(with index: Int) {
        childVCCollectioView.setContentOffset(CGPoint(x: CGFloat(index) * SCREEN_W, y: 0), animated: true)
    }
}

extension FansOrderIndex : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        sectionVcs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
        cell.contentView.subviews.forEach({$0.removeFromSuperview()})
        let v = sectionVcs[indexPath.row]
        cell.contentView.addSubview(v.view)
        v.view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return collectionView.zm.size
    }
        
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !(scrollView.isTracking || scrollView.isDecelerating) {
            return;
        }
        var index = Int(scrollView.contentOffset.x / scrollView.zm.width)
        
        if (Int(scrollView.contentOffset.x) % Int(scrollView.zm.width)) > Int(scrollView.zm.width / 2) {
            index += 1
        }
        seg.lineViewScrolle(to: index)
    }
}
